//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CommunityServer.Controls
{


    public class AdRepeater : RepeaterPlusNone
    {
        #region AdRepeaterLocation
        public enum AdRepeaterLocation
        {
            PreSecond, 
            PreSecondAndLast,
            Seperator,
            PreFooter,
            None
        }
        #endregion


        /// <summary>
        /// The template that is used to display an advertisment
        /// </summary>
        [
            Browsable( false ),
            DefaultValue( null ),
            Description( "Defines the ITemplate to be used for ads" ),
            PersistenceMode( PersistenceMode.InnerProperty ),
        ]
        public virtual ITemplate AdTemplate 
        {
            get { return _adTemplate; }
            set { _adTemplate = value; }
        }
        private ITemplate _adTemplate;

        private AdRepeaterLocation _location = AdRepeaterLocation.PreSecond;
        public AdRepeaterLocation Location
        {
            get{ return _location;}
            set{_location = value;}
        }

        //If templates are not defined, we do not get a chance to wire
        //wire up the AdTemplate in InitializeItem
        protected override void CreateControlHierarchy(bool useDataSource)
        {
            if(this.AdTemplate != null)
            {

                if(this.SeparatorTemplate == null)
                    this.SeparatorTemplate = new EmptyTemplate();

                if(this.FooterTemplate == null)
                    this.FooterTemplate = new EmptyTemplate();
            }

            base.CreateControlHierarchy (useDataSource);
        }


        #region EmptyTemplate
        private class EmptyTemplate : ITemplate
        {
            #region ITemplate Members

            public void InstantiateIn(Control container)
            {
            }

            #endregion
        }
        #endregion

        protected override void InitializeItem(RepeaterItem item)
        {
            
            if(item.ItemType == ListItemType.Separator)
            {
                if(this.Location == AdRepeaterLocation.Seperator || ((this.Location == AdRepeaterLocation.PreSecondAndLast || this.Location == AdRepeaterLocation.PreSecond) && item.ItemIndex == 0))
                {
                    if(this.AdTemplate != null)
                    {
                        this.AdTemplate.InstantiateIn(item);
                    }
                }

                if(this.SeparatorTemplate != null)
                {
                    this.SeparatorTemplate.InstantiateIn(item);
                }
            }
            else if(item.ItemType == ListItemType.Footer)
            {
                if(this.Location == AdRepeaterLocation.PreSecondAndLast || this.Location == AdRepeaterLocation.PreFooter)
                {
                    if(this.AdTemplate != null)
                        this.AdTemplate.InstantiateIn(item);
                }

                if(this.FooterTemplate != null)
                    this.FooterTemplate.InstantiateIn(item);
            }
            else
            { 
                base.InitializeItem (item);
            }
        }

    }
}